import NPogress from 'nprogress'
import 'nprogress/nprogress.css'
import { createRouter, createWebHistory } from 'vue-router'
import routes from './router'
NPogress.configure({
  ease: 'ease',
  speed: 500
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 白名单
const writeNames = ['/login']

router.beforeEach((to, from, next) => {
  NPogress.start()
  if (localStorage.getItem('token')) {
    if (to.path === '/login') {
      next('/')
    }
    return next()
  } else {
    if (writeNames.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach(() => {
  NPogress.done()
})

export default router
